package info.magnolia.blossom.sample.cv.service.impl;

import info.magnolia.blossom.sample.cv.model.CVSkills;
import info.magnolia.blossom.sample.cv.model.CurriculumVitae;
import info.magnolia.blossom.sample.cv.service.CVService;
import info.magnolia.blossom.sample.cv.service.MapperService;
import info.magnolia.blossom.sample.cv.service.OCMUtils;
import info.magnolia.jcr.util.SessionUtil;
import org.apache.jackrabbit.ocm.manager.ObjectContentManager;

import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: peterbalazs
 * Date: 2/20/13
 * Time: 3:21 PM
 * To change this template use File | Settings | File Templates.
 */
public class CVServiceMock implements CVService {

    private static final String CV1ID = "cv001";

    private final Map<String, CurriculumVitae> cache = new HashMap<String, CurriculumVitae>();

    @Override
    public List<CurriculumVitae> getAllCVs() {
        final List<CurriculumVitae> cvs = new ArrayList<CurriculumVitae>();
        cvs.add(getCV1());
        return cvs;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public List<CurriculumVitae> getAllCVs(String searchCriteria) {
        return getAllCVs();
    }

    @Override
    public CurriculumVitae getCV(String id) {

        Object cvObj = OCMUtils.getObjectContentManager().getObject("/cv" + id);
        if (cvObj == null) {
            final CurriculumVitae cv1temp = getCV1();
            cv1temp.setPath("/cv" + id);
            final ObjectContentManager ocm = OCMUtils.getObjectContentManager();
            ocm.insert(cv1temp);
            ocm.save();
            cvObj = ocm.getObject("/cv" + id);
        }
        cache.put(id, (CurriculumVitae) cvObj);

        final CurriculumVitae cacheCv = cache.get(id);
        if (cacheCv != null) {
            return cacheCv;
        }
        if (!CV1ID.equalsIgnoreCase(id)) {
            return null;
        }
        final CurriculumVitae cv1 = getCV1();
        cache.put(id, cv1);
        return cv1;
    }

    @Override
    public void addCV(CurriculumVitae cv) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void udpateCV(CurriculumVitae cv) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void removeCV(CurriculumVitae cv) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void updateTimeSpentAtEmployer(CurriculumVitae cv) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    private static CurriculumVitae getCV1() {
        final CurriculumVitae cv1 = new CurriculumVitae();
        cv1.setId("cv001");
        cv1.setName("John McKain");
        cv1.setCurrentDepartment("Java");
        cv1.setCurrentRole("Developer");
        List<CVSkills> languages = new ArrayList<CVSkills>();
        CVSkills lang1 = new CVSkills();
        lang1.setName("French");
        lang1.setLevel("beginner");
        CVSkills lang2 = new CVSkills();
        lang2.setName("English");
        lang2.setLevel("Advanced");
        languages.add(lang2);
        languages.add(lang1);
        cv1.setLanguages(languages);
        final Calendar cal = GregorianCalendar.getInstance();
        cal.add(Calendar.YEAR, -40);
        cal.add(Calendar.DAY_OF_YEAR, 127);
        cal.add(Calendar.MINUTE, 277);
        cv1.setDateOfBirth(cal.getTime());
        cv1.setAddress("Chicago, IL");
        return cv1;
    }

}
